package com.com.hemoWork.work2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;


//在客户与服务器成功建立连接后在控制台给出用户提示：请输入要上传的文件完整路径 如d:\aa.txt
public class TcpServerFile {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8089);
        Socket socket = serverSocket.accept();
        //接收客户端
        InputStream is = socket.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(is);

        OutputStream os = new FileOutputStream(new File("F:\\a2.txt"));
        Writer writer = new OutputStreamWriter(os);

        FileOutputStream fileOutputStream = new FileOutputStream("F:\\a2.txt", true);

        char[] buffer = new char[1024];
        int len = -1;

        while ((len = inputStreamReader.read(buffer)) != -1) {
            // writer.write(buffer, 0, len);
            String str = new String(buffer, 0, len);
            System.out.println("服务端接收数据：" + str);
            //将读到的str写入文件中
            fileOutputStream.write(str.getBytes());

        }
        fileOutputStream.close();
        inputStreamReader.close();
        os.close();


    }
}
